package app.auto.runner.base;

import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcessUtil {

    public static String getAdbInfo(String shellbehind) {
        ProcessBuilder pb = new ProcessBuilder();

        try {

            String cmd = shellbehind;

            Process process = pb.command(cmd.split(" ")).start();

            process.waitFor();
            StringBuilder successMsg = new StringBuilder();
            StringBuilder errorMsg = new StringBuilder();
            BufferedReader successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                successMsg.append(s);
            }
            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s);
            }
            if (!TextUtils.isEmpty(successMsg.toString())) {
                return successMsg.toString();
            }
            if (!TextUtils.isEmpty(errorMsg.toString())) {
                return errorMsg.toString();
            }

        } catch (Exception e) {
            return e.getCause() + " " + e.getMessage();
        }
        return "";
    }
}
